package com.ruoyi.web.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.domain.entity.Project;
import com.ruoyi.project.domain.entity.ProjectParticipan;
import com.ruoyi.project.domain.query.ProjectParticipanQuery;
import com.ruoyi.project.domain.query.ProjectQuery;
import com.ruoyi.project.service.IProjectParticipanService;
import com.ruoyi.project.service.IProjectService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.impl.SysPostServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/project/participan")
@Api(tags = "项目信息", description = "项目基本信息")
public class ProjectParticipanController {


    @Resource
    private IProjectParticipanService iProjectService;

    @Resource
    private SysPostServiceImpl sysPostService;

    @ApiOperation("列表")
    @GetMapping(value = "/list")
    public R getList(ProjectParticipanQuery query) {
        System.out.println(query);
        return R.ok(iProjectService.getList(query));
    }


    @ApiOperation("检查有没有添加销售负责人")
    @GetMapping(value = "/checkSale")
    public boolean checkSale(ProjectParticipanQuery query) {
       return iProjectService.checkSale(query);
    }


    @ApiOperation("获取所有销售负责人")
    @GetMapping(value = "/getSaleRole")
    public R getSaleRole(ProjectParticipanQuery query) {
        return R.ok(iProjectService.getSaleRole(5L));
    }

    @ApiOperation("获取所有销售负责人")
    @GetMapping(value = "/getSaleRoleXS")
    public R getSaleRoleXS(ProjectParticipanQuery query) {
        return R.ok(iProjectService.getSaleRoleXs(5L));
    }


    @GetMapping(value = "/getPermission")
    public R getPermission(ProjectParticipanQuery query) {
        return R.ok(iProjectService.getPermission(query));
    }

    //是否是销售负责人或项目经理
    @GetMapping(value = "/getPermissionSaleAndManage")
    public R getPermissionSaleAndManage(ProjectParticipanQuery query) {
        return R.ok(iProjectService.getPermissionSaleAndManage(query));
    }


    @GetMapping(value = "/getPostPermission")
    public R getPostPermission(ProjectParticipanQuery query) {
        return R.ok(iProjectService.getPostPermission(query));
    }

    @ApiOperation("新增")
    @PostMapping(value = "/save")
    public R add(@RequestBody ProjectParticipan project) {
        return R.ok(iProjectService.addInfo(project));
    }


    @ApiOperation("新增")
    @PostMapping(value = "/autoAddPerson")
    public R autoAddPerson(@RequestBody ProjectParticipan project) {

        return R.ok(iProjectService.addInfo(project));
    }


    @ApiOperation("修改")
    @PutMapping(value = "/update")
    public R update(@RequestBody ProjectParticipan project) {
        return R.ok(iProjectService.updateInfo(project));
    }


    @ApiOperation("删除")
    @DeleteMapping(value = "/{id}")
    public R delete(@PathVariable("id") String id) {
        return R.ok(iProjectService.deleteInfo(id));
    }


    @ApiOperation("详情")
    @GetMapping(value = "/details")
    public R<ProjectParticipan> getDetails(@RequestParam String id) {
        return R.ok(iProjectService.getById(id));
    }


    @GetMapping(value = "/indexPermisson")
    public Boolean getIndexPermission(ProjectParticipanQuery query) {
        if (SecurityUtils.hasRole("admin") || SecurityUtils.hasRole("gjadmin")) {
            System.out.println("当前用户有admin角色权限");
            return true;//如果是管理员 返回false
        }
        Long userId = SecurityUtils.getLoginUser().getUserId();
        List<Long> longs = sysPostService.selectPostListByUserId(userId);
        Boolean result = false;
        for (Long id : longs) {
            if (id.equals(2L) || id.equals(5L)) {
                result = true;
                break;
            }
        }
        return result;
    }

}
